package com.ydd.service.impl;

import com.ydd.constant.MessageConstant;
import com.ydd.dto.MenuDTO;
import com.ydd.entity.SysMenu;
import com.ydd.exception.BaseException;
import com.ydd.mapper.CateMapper;
import com.ydd.mapper.MenuMapper;
import com.ydd.service.MenuService;
import com.ydd.vo.MenuVO;
import com.ydd.vo.SysMenuChildVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private CateMapper cateMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 更新菜单
     *
     * @param menuDTO
     */
    @Override
    public void update(MenuDTO menuDTO) {
        //把dto封装进menu实体类
        if (Objects.isNull(menuDTO)) {
            throw new BaseException(MessageConstant.Parameter_Empty);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(menuDTO, sysMenu);
        //补充完整时间信息
        sysMenu.setUpdateDate(LocalDateTime.now());
        //更新数据库menu表
        menuMapper.update(sysMenu);
    }

    /**
     * 根据id删除菜单信息
     *
     * @param id
     */
    @Override
    public void del(Integer id) {
        if (Objects.isNull(id)) {
            throw new BaseException(MessageConstant.Parameter_Empty);
        }
        menuMapper.del(id);
    }

    /**
     * 查询所有菜单信息
     *
     * @return
     */
    @Override
    public List<MenuVO> allData() {
        //调用mapper返回list
        List<SysMenu> sysMenuList = menuMapper.allData();
        if (Objects.isNull(sysMenuList)) {
            throw new BaseException(MessageConstant.Parameter_Empty);
        }
        //装总的list数据
        List<MenuVO> menuVOList = new ArrayList<>();
        //子list
        //SysMenuChildVO[] SysMenuChildVoArray = (SysMenuChildVO[])sysMenuChildVOList.toArray();
        //判断是否为空
        if (sysMenuList != null) {
            //把pid为0的list作为第一层，封装到List<SysMenu>
            for (SysMenu sysMenu : sysMenuList) {
                if (sysMenu.getPid() == 0) {
                    //父0
                    MenuVO menuVO = new MenuVO();
                    BeanUtils.copyProperties(sysMenu, menuVO);//0
                    menuVO.setChildren(setCh(menuVO.getId(), sysMenuList));
                    /*List<SysMenuChildVO> sysMenuChildVOList1 = new ArrayList<>();
                    for (SysMenu menu : sysMenuList) {
                        if (menu.getPid().equals(sysMenu.getId())) {  //1
                            SysMenuChildVO sysMenuChildVO1 = new SysMenuChildVO();
                            BeanUtils.copyProperties(menu, sysMenuChildVO1);

                            //实现最里层chlid
                            List<SysMenu> sysMenuListLow = new ArrayList<>();
                            for (SysMenu Menuchild : sysMenuList) {
                                if (Menuchild.getPid().equals(menu.getId())) {
                                    sysMenuListLow.add(Menuchild);
                                }
                            }
                            Object[] sysMenus = sysMenuListLow.toArray();
                            sysMenuChildVO1.setChildren(sysMenus);
                            sysMenuChildVOList1.add(sysMenuChildVO1);
                        }
                    }
                    //遍历每一个List<SysMenu>把pid为第一层的放在第一层list中，作为第一层的里面list的参数
                    Object[] sysMenuChildVOS = sysMenuChildVOList1.toArray();
                    menuVO.setChildren(sysMenuChildVOS);*/
                    menuVOList.add(menuVO);
                }
            }
        }

        //返回数据
        menuVOList.toArray();
        return menuVOList;
    }

    /**
     * 递归算法
     *
     * @param pid
     * @param sysMenus
     * @return
     */
    public Object[] setCh(Integer pid, List<SysMenu> sysMenus) {

        List<SysMenuChildVO> father = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getPid().equals(pid)) {
                SysMenuChildVO child = new SysMenuChildVO();
                SysMenu fatherMenu = menuMapper.selectById(sysMenu.getId());
                BeanUtils.copyProperties(fatherMenu, child);
                child.setChildren(setCh(child.getId(), sysMenus));
                father.add(child);
            }
        }
        return father.toArray();
    }

    /**
     * 添加菜单数据
     *
     * @param menuDTO
     */
    @Override
    public void add(MenuDTO menuDTO) {
        //把DTO 放入实体类
        if (Objects.isNull(menuDTO)) {
            throw new BaseException(MessageConstant.Parameter_Empty);
        }
        List<SysMenu> sysMenuList = menuMapper.all();
        for (SysMenu Menu : sysMenuList) {
            if (Menu.getPath().equals(menuDTO.getPath())) {
                SysMenu sysMenu = new SysMenu();
                BeanUtils.copyProperties(menuDTO, sysMenu);
                sysMenu.setDelFlag(0);
                sysMenu.setCreateDate(LocalDateTime.now());
                sysMenu.setUpdateDate(LocalDateTime.now());
                //mapper执行添加方法
                menuMapper.add(sysMenu);
                return;
            }
        }
        throw new BaseException(MessageConstant.ADDRESS_IS_ERROR);
    }

    /**
     * 根据id查看详细信息
     *
     * @param id
     * @return
     */
    @Override
    public SysMenu rowInfo(Integer id) {

        if (Objects.isNull(id)) {
            throw new BaseException(MessageConstant.Parameter_Empty);
        }
        //mapper方法调用返回数据
        SysMenu sysMenu = menuMapper.selectById(id);
        return sysMenu;
    }
}
